package com.yhsoft.wxprogram.controller;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.yhsoft.buyer.controller.response.WxLoginOutVo;
import com.yhsoft.buyer.entity.BuyerUser;
import com.yhsoft.buyer.service.IBuyerUserService;
import com.yhsoft.common.service.TokenService;
import com.yhsoft.common.vo.Result;
import com.yhsoft.mall.controller.request.TopicGeneralInVo;
import com.yhsoft.mall.controller.response.ModuleInfoOutVo;
import com.yhsoft.mall.controller.response.TopicInfoOutVo;
import com.yhsoft.mall.service.IModuleInfoService;
import com.yhsoft.mall.service.ITopicInfoService;
import com.yhsoft.sys.entity.BaicContent;
import com.yhsoft.sys.entity.SearchHistory;
import com.yhsoft.sys.service.IBaicContentService;
import com.yhsoft.sys.service.ISearchHistoryService;
import com.yhsoft.wxprogram.controller.request.GetPhoneNumInVo;
import com.yhsoft.wxprogram.controller.request.UserInfoInVo;
import com.yhsoft.wxprogram.controller.request.WxLoginInVo;
import com.yhsoft.wxprogram.controller.response.GetSearchHistoryOutVo;
import com.yhsoft.wxprogram.controller.response.UserPhoneNumOutVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 微信小程序端接口
 * </p>
 *
 * @author yyyyyyyh
 * @since 2023-09-22
 */
@Api(tags = {"G-wx-登陆及首页接口"})
@RestController
@RequestMapping("/wxApi")
public class WxMallFristPageController {

    @Autowired
    private IBuyerUserService iBuyerUserService;

    @Resource
    private IModuleInfoService iModuleInfoService;

    @Autowired
    private ISearchHistoryService iSearchHistoryService;

    @Autowired
    private IBaicContentService iBaicContentService;

    @Resource
    private ITopicInfoService iTopicInfoService;

    // login
    @ApiOperation("登录（小程序）")
    @PostMapping("/wxLogin")
    public Result<WxLoginOutVo> wxLogin(@RequestBody WxLoginInVo inVo) {
        WxLoginOutVo res = iBuyerUserService.loginByWx(inVo.getAppId(), inVo.getCode(), inVo.isTest());
        return Result.success(res);
    }

    @ApiOperation("手机号授权")
    @PostMapping("/getPhoneNum")
    public Result<?> getPhoneNum(@RequestBody GetPhoneNumInVo inVo) {
        iBuyerUserService.getPhoneNum(inVo);
        return Result.success();
    }

    @ApiOperation("查询用户手机号")
    @GetMapping("/getUserPhoneNum")
    public Result<UserPhoneNumOutVo> getUserPhoneNum() {
        String phoneNum = iBuyerUserService.getUserPhoneNum();
        if (StringUtils.isEmpty(phoneNum)) {
            return Result.fail("手机号为空，请先授权！");
        }
        UserPhoneNumOutVo outVo = new UserPhoneNumOutVo();
        outVo.setPhoneNum(phoneNum);
        return Result.success(outVo);
    }

    @ApiOperation("保存头像和昵称")
    @PostMapping("/setUserInfo")
    public Result<?> setUserInfo(@RequestBody UserInfoInVo inVo) {
        iBuyerUserService.setUserInfo(inVo);
        return Result.success();
    }

    @ApiOperation("获取头像和昵称")
    @PostMapping("/getUserInfo")
    public Result<UserInfoInVo> getUserInfo(@RequestParam(value = "openId") String openId) {
        // 获取昵称，头像
        LambdaQueryWrapper wrapper = new LambdaQueryWrapper<BuyerUser>()
                .eq(BuyerUser::getOpenId, openId);
        BuyerUser user = iBuyerUserService.getBaseMapper().selectOne(wrapper);
        if (user == null) {
            return Result.fail("用户不存在");
        }
        UserInfoInVo outvo = BeanUtil.copyProperties(user, UserInfoInVo.class);
        return Result.success(outvo);

    }

    @ApiOperation("获取小程序联系电话")
    @GetMapping("/getPhone")
    public Result<UserPhoneNumOutVo> getPhone(@RequestParam(value = "appKey") String appKey) {
        LambdaQueryWrapper<BaicContent> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(BaicContent::getAppId, appKey);
        BaicContent baicContent = iBaicContentService.getBaseMapper().selectOne(queryWrapper);
        if (baicContent == null) {
            return Result.fail("未配置手机号!");
        }
        if (StringUtils.isNotEmpty(baicContent.getServiceTelNum())) {
            UserPhoneNumOutVo outVo = new UserPhoneNumOutVo();
            outVo.setPhoneNum(baicContent.getServiceTelNum());
            return Result.success(outVo);
        }
        return Result.fail("未配置手机号!");
    }

    @ApiOperation("获取搜索历史")
    @GetMapping("/getSearchHistory")
    public Result<GetSearchHistoryOutVo> getSearchHistory() {
        GetSearchHistoryOutVo outVo = new GetSearchHistoryOutVo();
        List<String> list = new ArrayList<>();
        LambdaQueryWrapper wrapper = new LambdaQueryWrapper<SearchHistory>()
                .eq(SearchHistory::getUserId, TokenService.getUserBytoken().getId());
        List<SearchHistory> list1 = iSearchHistoryService.getBaseMapper().selectList(wrapper);
        if (list1.size() > 0) {
            list1.forEach(a -> {
                list.add(a.getKeyword());
            });
        }
        outVo.setSearchHistorys(list);
        return Result.success(outVo);
    }

    @ApiOperation("清空搜索历史")
    @GetMapping("/delSearchHistory")
    public Result<?> delSearchHistory() {
        LambdaQueryWrapper wrapper = new LambdaQueryWrapper<SearchHistory>()
                .eq(SearchHistory::getUserId, TokenService.getUserBytoken().getId());
        iSearchHistoryService.getBaseMapper().delete(wrapper);
        return Result.success("删除成功！");
    }

    @ApiOperation("获得模块内容")
    @GetMapping("/getModuleInfo")
    public Result<ModuleInfoOutVo> getModuleInfo() {
        ModuleInfoOutVo outVo = iModuleInfoService.getModuleInfo("");
        return Result.success(outVo);
    }

    @ApiOperation("获得通用专题模块内容")
    @GetMapping("/getTopicGeneralInfo")
    public Result<TopicGeneralInVo> getTopicGeneralInfo(@RequestParam(value = "id", required = true) Integer id){
        TopicGeneralInVo vo =  iTopicInfoService.getTopicGeneralInfo(id);
        return Result.success(vo);
    }

    @ApiOperation("获得充值专题配置")
    @GetMapping("/getTopUpTopicInfo")
    public Result<TopicInfoOutVo> getTopUpTopicInfo(@RequestParam(value = "id", required = true) Integer id){
        TopicInfoOutVo outVo =  iTopicInfoService.getTopUpTopicInfo(id);
        return Result.success(outVo);
    }
}
